<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
    <script src="https://cdn.jsdelivr.net/npm/vue@2"></script>
    <script src="http://libs.baidu.com/jquery/2.0.0/jquery.min.js"></script>
    <link rel="stylesheet" href="https://unpkg.com/element-ui/lib/theme-chalk/index.css">
    <script src="https://unpkg.com/element-ui/lib/index.js"></script>
</head>
<body>
<div id="app" >
    <el-table
            border
            :data="officeInfos"
    >
        <el-table-column
                prop="dept.dname"
                label="所属部门"
                width="200">
            <template slot="header" slot-scope="scope">
                <el-select v-model="depts.id" placeholder="请选择科室所属部门" @change="listDept(depts.id)">
                    <el-option
                            v-for="item in depts"
                            :key="item.id"
                            :label="item.dname"
                            :value="item.id">
                    </el-option>
                </el-select>
            </template>
        </el-table-column>
        <el-table-column
                prop="oname"
                label="科室名称"
                >
        </el-table-column>
        <el-table-column
                prop="ointro"
                label="科室简介"
                >
        </el-table-column>
        <el-table-column
                prop="dsname.state"
                label="科室状态"
                >
        </el-table-column>
        <el-table-column
                prop=""
                >
            <template slot="header" slot-scope="scope">
                <el-button type="success" plain  @click="addDialog()">添加科室</el-button>
            </template>
            <template slot-scope="scope">
                <el-button type="success" icon="el-icon-edit" circle  @click="getOfficeInfo(scope.row.id)"></el-button>
                <!--<el-button type="danger" icon="el-icon-delete" circle @click="deleteOfficeInfo(scope.row.id)"></el-button>-->
            </template>
        </el-table-column>
    </el-table>

    <el-dialog title="添加科室" :visible.sync="addDialogFormVisible">
        <el-form id="addAdminOfficeFrom" :model="getOffice" :rules="rules" ref="officeInfos">
            <el-form-item label="科室名称" prop="oname" >
                <el-input v-model="getOffice.oname" name="oname" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="科室简介" prop="ointro" >
                <el-input v-model="getOffice.ointro" name="ointro" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="科室成就" prop="oresult" >
                <el-input v-model="getOffice.oresult" name="oresult" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="学术成就" prop="oacademic" >
                <el-input v-model="getOffice.oacademic" name="oacademic" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="技术优势" prop="oskill" >
                <el-input v-model="getOffice.oskill" name="oskill" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="就诊须知" prop="oknow" >
                <el-input v-model="getOffice.oknow" name="oknow" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="请选择科室状态" prop="state">
                <template>
                    <select style="width: 90px;" v-model="getOffice.state" placeholder="请选择科室状态" name = "state">
                        <option  v-for="(g,i) in ostate"
                                 :label="g.state"
                                 :value="g.id">
                        </option>
                    </select>
                </template>
            </el-form-item>
            <el-form-item label="请选择部门" prop="did">
                <template>
                    <select style="width: 90px;" v-model="getOffice.did" placeholder="请选择部门" name = "did">
                        <option  v-for="(g,i) in depts"
                                 :label="g.dname"
                                 :value="g.id">
                        </option>
                    </select>
                </template>
            </el-form-item>
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button @click="addDialogFormVisible = false">取 消</el-button>
            <el-button type="primary" @click="addOffice()">添 加</el-button>
        </div>
    </el-dialog>

    <el-dialog title="修改科室信息" :visible.sync="dialogFormVisible">
        <el-form id="updateAdminOfficeFrom" :model="getOffice" :rules="rules" ref="officeInfos">
            <el-form-item label="编号" prop="id" style="display: none">
                <el-input v-model="getOffice.id" name="id" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="科室名称" prop="oname" >
                <el-input v-model="getOffice.oname" name="oname" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="科室简介" prop="ointro" >
                <el-input v-model="getOffice.ointro" name="ointro" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="科室成就" prop="oresult" >
                <el-input v-model="getOffice.oresult" name="oresult" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="学术成就" prop="oacademic" >
                <el-input v-model="getOffice.oacademic" name="oacademic" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="技术优势" prop="oskill" >
                <el-input v-model="getOffice.oskill" name="oskill" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="就诊须知" prop="oknow" >
            <el-input v-model="getOffice.oknow" name="oknow" autocomplete="off"></el-input>
            </el-form-item>
            <el-form-item label="请选择科室状态" prop="state">
                <template>
                    <select style="width: 90px;" v-model="getOffice.state" placeholder="请选择科室状态" name = "state">
                        <option  v-for="(g,i) in ostate"
                                 :label="g.state"
                                 :value="g.id">
                        </option>
                    </select>
                </template>
            </el-form-item>
            <el-form-item label="请选择部门" prop="did">
                <template>
                    <select style="width: 90px;" v-model="getOffice.did" placeholder="请选择部门" name = "did">
                        <option  v-for="(g,i) in depts"
                                 :label="g.dname"
                                 :value="g.id">
                        </option>
                    </select>
                </template>
            </el-form-item>
            </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
            <el-button @click="dialogFormVisible = false">取 消</el-button>
            <el-button type="primary" @click="updateOffice()">修 改</el-button>
        </div>
    </el-dialog>

</div>
<script>
    $.get("/demo/office/queryOffice",function (backData) {
        haha.officeInfos = backData.data;
    });
    $.get("/demo/office/queryDept",function (backData) {
        haha.depts = backData.data;
    });
    $.get("/demo/office/queryState",function (backData) {
        haha.ostate = backData.data;
    });

    var haha = new Vue({
        el: "#app",
        data: {
            ostate:{},
            depts:{},
            dename:"",
            dstate:"",
            dialogFormVisible:false,
            addDialogFormVisible:false,
            officeInfos:[],
            getOffice:{},
            rules:{
                oname: [
                    { required: true, message: '请输入科室名称', trigger: 'blur' }
                ],
                ointro: [
                    { required: true, message: '请输入部门简介（可写暂无）', trigger: 'blur' }
                ],
                state: [
                    { required: true, message: '请选择科室状态', trigger: 'blur' }
                ],
                did: [
                    { required: true, message: '请选择科室所属部门', trigger: 'blur' }
                ]
            }
        },
        methods: {
            addOffice(){
                haha.$refs["officeInfos"].validate((valid) => {
                    if (valid) {
                        var addOffice = new FormData(addAdminOfficeFrom);
                        $.ajax({
                            url:"/demo/office/addOffice",
                            type:"POST",
                            data:addOffice,
                            processData:false,
                            contentType:false,
                            success:function(backData){
                                if (backData.code == 1){
                                    haha.dialogFormVisible=false;
                                    haha.$message.success('添加成功');
                                    window.location.reload();
                                } else{
                                    haha.$message.error(backData.msg);
                                }
                            }
                        });
                    }
                });
            },
            addDialog(){
                haha.addDialogFormVisible = true;
            },
            deleteOfficeInfo(id){
                haha.$confirm('此操作将永久删除该科室, 是否继续?', '温馨提示', {
                    confirmButtonText: '确定',
                    cancelButtonText: '取消',
                    type: 'warning'
                }).then(() => {
                    $.get("/demo/office/deleteOffice/"+id,function (backData) {
                        if (backData.code == 1){
                            haha.$message.success('删除成功');
                            window.location.reload();
                        }else{
                            haha.$message.error(backData.msg);
                        }
                    });
                });

            },
            updateOffice(){
                haha.$refs["officeInfos"].validate((valid) => {
                    if (valid) {
                        var updateOffice = new FormData(updateAdminOfficeFrom);
                        $.ajax({
                            url:"/demo/office/updateOffice",
                            type:"POST",
                            data:updateOffice,
                            processData:false,
                            contentType:false,
                            success:function(backData){
                                if (backData.code == 1){
                                    haha.dialogFormVisible=false;
                                    haha.$message.success('修改成功');
                                    window.location.reload();
                                } else{
                                    haha.$message.error(backData.msg);
                                }
                            }
                        });
                    }
                });
            },

            listDept(id){
                $.get("/demo/office/listOffice/"+id,function (backData) {
                    haha.officeInfos = backData.data;
                });
            },
            getOfficeInfo(id){
                $.get("/demo/office/getOffice/"+id,function (backData) {
                    haha.getOffice = backData.data;
                    haha.dstate = backData.data.dsname.state;
                    haha.dename =backData.data.dept.dname;
                });
                haha.dialogFormVisible = true
            }
        }
    })
</script>
</body>
</html>